package com.feng.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.feng.springboot.bean.User;
import com.feng.springboot.service.UserService;
import com.feng.springboot.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.thymeleaf.util.StringUtils;

import java.util.Arrays;
import java.util.List;

@Controller
public class TableController {

    @Autowired
    UserServiceImpl userService;

    @GetMapping("/basic_table")
    public String basic_table(){
        return "table/basic_table";
    }

    @GetMapping("/dynamic_table")
    public String dynamic_table(Model model,
                                @RequestParam(value = "pn",defaultValue = "1")Integer pn){
//        List<User> users = Arrays.asList(
//                new User(101, "feng", "feng"),
//                new User(102, "wen", "wen"),
//                new User(103, "冯", "feng"),
//                new User(104, "文", "wen"));
//        List<User> users = userService.list();
//        model.addAttribute("users",users);

        Page<User> userPage = new Page<>(pn,2);
//        Page<User> page = userService.page(userPage, null);
        Page<User> page = userService.page(userPage,null);
        model.addAttribute("page",page);
        return "table/dynamic_table";
    }

    @GetMapping("/editable_table")
    public String editable_table(){
        return "table/editable_table";
    }

    @GetMapping("/pricing_table")
    public String pricing_table(){
        return "table/pricing_table";
    }

    @GetMapping("/responsive_table")
    public String responsive_table(){
        return "table/responsive_table";
    }

    @GetMapping("/deleteUser/{id}")
    public String deleteUser(@PathVariable("id")Integer id,
                             @RequestParam(value = "pn",defaultValue = "1")Integer pn,
                             RedirectAttributes redirectAttributes){
        boolean b = userService.removeById(id);
        redirectAttributes.addAttribute("pn",pn);
        return "redirect:/dynamic_table";
    }
}